using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public static class HUDGlobal
    {
        static Camera m_mainCamera;

        public static void BindHUDMainCamera(Camera camera)
        {
            m_mainCamera = camera;
        }

        public static Camera GetHUDMainCamera()
        {
            if (m_mainCamera == null)
            {
                m_mainCamera = Camera.main;
            }

            return m_mainCamera;
        }

        static bool initialized = false;

        internal static Texture numTexture;

        public static void BindNumberTexture(Texture tex)
        {
            numTexture = tex;
        }

        internal static HUDNumberSetting[] hudNumSetting;
        internal static Color defultHUDNumberColor;
        internal static Dictionary<int, Color> HUDNumberColorDic;
        internal static float PlayerHurtHUDNumberscaleRatio;
        internal static float hudNumberDuration = 2f;
        internal static float numScaleBase;
        internal static float numTimeSpace;

        public static void InitHUDSetting(string hudConfigPath)
        {
            if (initialized)
            {
                return;
            }
            var resLoader = ResLoader.Create();

            var rConfig = resLoader.LoadABAsset<BattleHUDConfig>(hudConfigPath);
            hudNumberDuration = rConfig.numDuration;
            numScaleBase = rConfig.numScaleBase;
            numTimeSpace = rConfig.numTimeSpace;
            var nLen = (int)EHUDNumberRenderType.Max;
            hudNumSetting = new HUDNumberSetting[nLen];
            for (int i = 0; i < rConfig.NumberSetting.Length; i++)
            {
                var rSetting = rConfig.NumberSetting[i];
                hudNumSetting[(int)rSetting.hudType] = rSetting.setting;
            }

            var rDict = new Dictionary<int, string>();
            for (int i = 0; i < rConfig.NumberSpriteSetting.Length; i++)
            {
                var rSpecialSprite = rConfig.NumberSpriteSetting[i];
                rDict.Add((int)rSpecialSprite.hudType, rSpecialSprite.numSprite);
            }

            for (int i = 0; i < nLen; i++)
            {
                if (hudNumSetting[i] == null)
                {
                    hudNumSetting[i] = rConfig.defaultSetting;
                }

                if (!rDict.TryGetValue(i, out var rSpriteName))
                {
                    rSpriteName = rConfig.defaultNumSprite;
                }

                var type = i;
                m_numberStrPool[type] = new string[10];
                for (int j = 0; j < 10; j++)
                {
                    m_numberStrPool[type][j] = string.Concat(rSpriteName + "/" + rSpriteName, "_", j.ToString());
                }
            }
            // HUDNumberColorDic = new Dictionary<int, Color>();
            //
            // foreach (var hUDNumberColor in battleConfigAsset.HUDNumberColorList)
            // {
            //     int colorKey = (int)hUDNumberColor.HurtHealType + (int)hUDNumberColor.HurtType * 100;
            //     HUDNumberColorDic.Add(colorKey, hUDNumberColor.Color);
            // }
            // defultHUDNumberColor = battleConfigAsset.DefultColor;
            // PlayerHurtHUDNumberscaleRatio = battleConfigAsset.PlayerHurtHUDNumberscaleRatio;

            // for (int i = 0; i < count; i++)
            // {
            //     var baseCfg = GameData.instance.listFloatTextBase.GetDataByIndex(i);
            //     HUDNumberAnimationCurve curve = battleConfigAsset.GetHUDNumberAnimationCurve((HUDNumberRenderType)baseCfg.Id);
            //     var setting = new HUDNumberSetting();
            //     hudNumSetting[baseCfg.Id] = setting;
            //     
            //     setting.SpriteName = baseCfg.Icon;
            //     setting.ShowNumber = baseCfg.ShowNumber;
            //     setting.FontIndex = baseCfg.NumberFont;
            //     setting.SpriteGap = baseCfg.SpriteGap;
            //
            //     setting.ScaleCurve = curve.numberScaleCurve;
            //
            //     setting.AlphaCurve = curve.numberAlphaCurve;
            //     
            //     setting.MoveXCurve = curve.numberMoveXCurve;
            //
            //     setting.MoveYCurve = curve.numberMoveYCurve;
            // }
            resLoader.Put2Pool();
            initialized = true;
        }

        private static bool m_bAddUpdateLogic = false;
        internal static void AddLateUpdate(System.Action update)
        {
            if (m_bAddUpdateLogic)
                return;
            m_bAddUpdateLogic = true;
            ManagerCenter.MonoDriver.lateUpdateEvent += update;
        }

        internal static void RemoveLateUpate(System.Action update)
        {
            if (!m_bAddUpdateLogic)
                return;
            m_bAddUpdateLogic = false;
            ManagerCenter.MonoDriver.lateUpdateEvent -= update;
        }

        // public static float DesignScreenWidth => Screen.width;
        // public static float DesignScreenHeight => Screen.height;

        public static float CameraNearDist => 6.5f;
        public static float CameraFarDist => 50f;
        public static float NumberScaleMin => 0.1f;
        public static float NumberScaleMax => 1.2f;

        //数字string池,避免每次拼接创建string
        internal static Dictionary<int, string[]> m_numberStrPool = new Dictionary<int, string[]>();

        static HUDGlobal()
        {
            HUDMesh.shaderLoad = (path) => ManagerCenter.Render.GetShader(path);
            HUDRenderPassFeature.GetHUDMainCamera = GetHUDMainCamera;
        }

        // public static Color GetHUDNumberColor(eHurtHealType hurtHealType, HurtType skillType)
        // {
        //     int key = (int)hurtHealType + (int)skillType * 100;
        //     if (HUDNumberColorDic.ContainsKey(key))
        //     {
        //         return HUDNumberColorDic[key];
        //     }
        //     return defultHUDNumberColor;
        // }

        public static string GetSpriteName(int type, int num)
        {
            return m_numberStrPool[type][num];
        }

    }
}